package com.cai310.lottery.web.controller.lottery.jclq;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import net.sf.json.JSONObject;

import com.cai310.lottery.Constant;
import com.cai310.lottery.exception.DataException;
import com.cai310.lottery.support.ContentBean;
import com.cai310.lottery.support.UnitsCountUtils;
import com.cai310.lottery.support.jclq.JclqCompoundContent;
import com.cai310.lottery.support.jclq.JclqMatchItem;
import com.cai310.lottery.support.jclq.PassMode;
import com.cai310.lottery.support.jclq.PassType;
import com.cai310.lottery.support.jclq.SchemeType;
import com.cai310.lottery.web.controller.lottery.SchemeCreateForm;

public class JclqSchemeCreateForm extends SchemeCreateForm {

	// ------------------------ 复式 -----------------------

	/** 选择的场次内容 */
	private List<JclqMatchItem> items;

	/** 胆码最小命中数 */
	private Integer danMinHit;

	/** 胆码最大命中数 */
	private Integer danMaxHit;

	// --------------------- 单复式共用 --------------------

	/** 过关方式 */
	private List<PassType> passTypes;

	/** 方案类型 */
	private SchemeType schemeType;
	
	/** 过关类型 */
	private PassMode passMode;

	// ----------------------------------------------------

	@Override
	protected ContentBean buildCompoundContentBean() throws DataException {
		// 在Controller那边做一些数据验证

		Collections.sort(this.items);
		PassType minPassType = passTypes.get(0);// 最小的过关方式

		final List<JclqMatchItem> danList = new ArrayList<JclqMatchItem>();
		final List<JclqMatchItem> unDanList = new ArrayList<JclqMatchItem>();
		for (JclqMatchItem item : items) {
			if (item.isDan())
				danList.add(item);
			else
				unDanList.add(item);
		}

		// 检查设胆数目
		if (danList.size() > minPassType.getMatchCount()) {
			throw new DataException("设置的胆码不能大于最小的过关场次.");
		}

		if (danMinHit == null || danMinHit <= 0)
			danMinHit = danList.size();
		else if (danMinHit > danList.size())
			throw new DataException("模糊设胆不正确.");

		if (danMinHit + unDanList.size() < minPassType.getMatchCount())
			throw new DataException("模糊设胆不正确.");

		if (danMaxHit == null || danMaxHit <= 0)
			danMaxHit = danList.size();

		int units = 0;
		for (PassType passType : this.passTypes) {
			for (final int needs : passType.getPassMatchs()) {
				units += UnitsCountUtils.countUnits(needs, danList, danMinHit, danMaxHit, unDanList);
				if (units > Constant.MAX_UNITS)
					throw new DataException("复式方案单倍注数不能大于" + Constant.MAX_UNITS + "注.");
			}
		}

		JclqCompoundContent content = new JclqCompoundContent();
		content.setItems(items);
		content.setDanMinHit(danMinHit);
		content.setDanMaxHit(danMaxHit);

		return new ContentBean(units, JSONObject.fromObject(content).toString());
	}

	@Override
	protected ContentBean buildSingleContentBean() throws DataException {
		throw new DataException("竞彩足球目前暂时不支持单式投注.");
	}

	// ================================================================

	/**
	 * @return {@link #items}
	 */
	public List<JclqMatchItem> getItems() {
		return items;
	}

	/**
	 * @param items the {@link #items} to set
	 */
	public void setItems(List<JclqMatchItem> items) {
		this.items = items;
	}

	/**
	 * @return {@link #passTypes}
	 */
	public List<PassType> getPassTypes() {
		return passTypes;
	}

	/**
	 * @param passTypes the {@link #passTypes} to set
	 */
	public void setPassTypes(List<PassType> passTypes) {
		this.passTypes = passTypes;
	}

	/**
	 * @return {@link #danMinHit}
	 */
	public Integer getDanMinHit() {
		return danMinHit;
	}

	/**
	 * @param danMinHit the {@link #danMinHit} to set
	 */
	public void setDanMinHit(Integer danMinHit) {
		this.danMinHit = danMinHit;
	}

	/**
	 * @return {@link #danMaxHit}
	 */
	public Integer getDanMaxHit() {
		return danMaxHit;
	}

	/**
	 * @param danMaxHit the {@link #danMaxHit} to set
	 */
	public void setDanMaxHit(Integer danMaxHit) {
		this.danMaxHit = danMaxHit;
	}

	/**
	 * @return the schemeType
	 */
	public SchemeType getSchemeType() {
		return schemeType;
	}

	/**
	 * @param schemeType the schemeType to set
	 */
	public void setSchemeType(SchemeType schemeType) {
		this.schemeType = schemeType;
	}
	public PassMode getPassMode() {
		return passMode;
	}
	public void setPassMode(PassMode passMode) {
		this.passMode = passMode;
	}
}
